package com.fudan.biz.etl.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.RunApplication;
import com.fudan.biz.etl.container.LocalTaskContainer;
import com.fudan.biz.etl.entity.EtlDetail;
import com.fudan.biz.etl.service.EtlService;
import com.fudan.cfg.RunConfiguration;
import com.fudan.framework.base.DaoService;
import com.fudan.framework.entity.ResponseMsg;
import com.fudan.framework.entity.TableJson;
import com.fudan.framework.web.controller.AddController;
import com.fudan.framework.web.controller.DeleteController;
import com.fudan.framework.web.controller.FindController;
import com.fudan.framework.web.controller.PathController;
import com.fudan.framework.web.controller.UpdateController;

@Controller
@RequestMapping("/biz/etl")
public class EtlController implements PathController,AddController<EtlDetail>,FindController<EtlDetail>,UpdateController<EtlDetail>,DeleteController<EtlDetail>{

	@Autowired
	private EtlService etlService;


	@Override
	public String pathPrefix() {
		return "biz/etl";
	}

	@RequestMapping("/list")
	@ResponseBody
	public TableJson configTable(HttpServletRequest request) {
		return TableJson.build(etlService.pages(request), etlService.countAll());
	}

	@RequestMapping("/submit")
	@ResponseBody
	public ResponseMsg submitTask(HttpServletRequest request) {
		String taskId = request.getParameter("id");
		EtlDetail detail = etlService.get(taskId);
		try {
			RunConfiguration runConfiguration = RunApplication.buildRunConfiguration(detail.getConfig());
			String mode = runConfiguration.getRunProperties().getStringConfigOrDefault("mode", "local");
			if("local".equals(mode)) {
				Integer state = detail.getState();
				if(state == 1) {
					return ResponseMsg.build(501, "该任务正在执行");
				}
				if(state == 2) {
					return ResponseMsg.build(501, "该任务已经执行");
				}
				LocalTaskContainer taskContainer = LocalTaskContainer.apply(detail);
				taskContainer.start();
				return ResponseMsg.build(0, taskContainer.getId());
			}else if("remote".equals(mode)) {
				return ResponseMsg.build(0,"remote");
			}else {
				return ResponseMsg.build(501, "错误的mode设置:"+mode);
			}
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseMsg.build(500, "任务执行出错: "+e.getMessage());
		}
	}
	@RequestMapping("/test")
	@ResponseBody
	public ResponseMsg testTask(HttpServletRequest request) {
		String config = request.getParameter("config");
		try {
			RunConfiguration runConfiguration = RunApplication.buildRunConfiguration(config);
			String mode = runConfiguration.getRunProperties().getStringConfigOrDefault("mode", "local");
			if("local".equals(mode)) {
				LocalTaskContainer taskContainer = LocalTaskContainer.apply(config);
				taskContainer.start();
				return ResponseMsg.build(0, taskContainer.getId());
			}else if("remote".equals(mode)) {
				return ResponseMsg.build(0,"remote");
			}else {
				return ResponseMsg.build(501, "错误的mode设置:"+mode);
			}
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseMsg.build(500, "任务执行出错: "+e.getMessage());
		}
	}
	@RequestMapping("/clear/local_container")
	@ResponseBody
	public ResponseMsg clearContainer() {
		LocalTaskContainer.clear();
		return ResponseMsg.build(0);
	}

	@RequestMapping("/showForeach")
	@ResponseBody
	public ResponseMsg getTaskDetail(HttpServletRequest request) {
		String mode = request.getParameter("mode");
		String id = request.getParameter("id");
		if("local".equals(mode)) {
			LocalTaskContainer localTaskContainer = LocalTaskContainer.get(id);
			return ResponseMsg.build().putData(localTaskContainer.getOutput().toString());
		}else {
			return ResponseMsg.build(404, "没有找到任务");
		}
	}

	@Override
	public EtlDetail buildEntity(HttpServletRequest request) throws Exception {
		EtlDetail etlDetail = AddController.super.buildEntity(request);
		etlDetail.setState(0);
		return etlDetail;
	}

	@Override
	public DaoService<EtlDetail> injectService() {
		return etlService;
	}

}
